package com.ogawa.joypal.user.activity

import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.ogawa.base.base.BaseVMActivity
import com.ogawa.base.bean.LoadState
import com.ogawa.base.utils.AppManager
import com.ogawa.base.utils.AppUtil
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.projectcommon.utils.ProjectSpUtils
import com.ogawa.base.utils.imageloader.ImageConfigImpl
import com.ogawa.base.utils.imageloader.ImageLoader
import com.ogawa.base.utils.isEmptyStr
import com.ogawa.base.utils.printNoNull
import com.ogawa.base.utils.statusbar.SystemStatusBarUtil
import com.ogawa.joypal.user.R
import com.ogawa.joypal.user.viewmodel.UserInfoViewModel
import com.ogawa.projectcommon.constants.Constant
import com.ogawa.projectcommon.datacollect.DataCollectUtils
import com.ogawa.projectcommon.dialog.ShowUpdateDialog
import com.ogawa.projectcommon.utils.SingletonUtils
import com.ogawa.projectcommon.viewmodel.CheckUpdateModel
import kotlinx.android.synthetic.main.activity_user_info.*


@Route(path = UrlPathConstant.ACTIVITY_USER_INFO)
public class UserInfoActivity : BaseVMActivity<UserInfoViewModel>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_user_info
    }

    override fun initView() {
        super.initView()
        SystemStatusBarUtil.setStatusBar(this, R.color.colorTransparent, true)
        backIv.setOnClickListener { finish() }
        softVersion.text = getString(R.string.user_version_name) + " ${AppUtil.getVersionName()}"
        suggestLL.setOnClickListener {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_USER_SUGGEST).navigation()
        }
        editClickGroupLL.setOnClickListener {
            val userInfo = ProjectSpUtils.getUserInfo()
            if (userInfo.isVisitor) {
//                ToastUtils.showShort(getString(R.string.visitor_no_pro_edit_user))
                ProjectSpUtils.userInfoLoginOut()
                AppManager.getAppManager().finishAllActivity()
                ARouter.getInstance().build(UrlPathConstant.ACTIVITY_USER_LOGIN).navigation()

                return@setOnClickListener
            }
            ARouter.getInstance()
                .build(UrlPathConstant.ACTIVITY_USER_EDIT_INFO)
                .navigation()
        }
        loginOutBt.setOnClickListener {
            AlertDialog.Builder(mActivity)
                .setMessage(getString(R.string.re_login_out))
                .setNegativeButton(R.string.cancel) { _, _ ->
                }
                .setPositiveButton(R.string.confirm) { _, _ ->
                    DataCollectUtils.logoutEvent()
                    ProjectSpUtils.userInfoLoginOut()
                    AppManager.getAppManager().finishAllActivity()
                    ARouter.getInstance().build(UrlPathConstant.ACTIVITY_USER_LOGIN).navigation()

                }
                .show()
        }
        aboutUsLL.setOnClickListener {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_WEB_VIEW)
                .withString("titleStr", resources.getString(R.string.user_about))
                .withString("url", Constant.ABOUT_US_URL)
                .navigation()
        }
        agreementLL.setOnClickListener {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_WEB_VIEW)
                .withString("titleStr", resources.getString(R.string.agreement))
                .withString("url", Constant.AGREE_MENT_URL)
                .navigation()
        }
        privacyPolicyLL.setOnClickListener {
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_USER_PRIVACY_POLICY)
                .navigation()
        }


        var mCheckUpdateModel = ViewModelProviders.of(this).get(CheckUpdateModel::class.java)
        mCheckUpdateModel.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Loading -> {
                    showLoadingDialog(canceledOnTouchOutside = false)
                }
                is LoadState.Fail -> {
                    dismissLoadingDialog()
                    ToastUtils.showShort(it.msg)
                }
                is LoadState.Success -> {
                    dismissLoadingDialog()
                }
            }
        })
        mCheckUpdateModel.mUpdateResponseBean.observe(this, Observer {
            dismissLoadingDialog()
            newVersionDot.visibility = View.GONE
            if (it.isupgrade == 1) {
                newVersionDot.visibility = View.VISIBLE
                var mShowUpdateDialog = ShowUpdateDialog(mActivity, it,
                    ShowUpdateDialog.ShowUpdateDialogCallBack {
                        it.dismiss()
                    })
                mShowUpdateDialog.show()
            } else {
                ToastUtils.showShort(getString(R.string.check_update_is_new))
            }

        })
        checkUpBt.setOnClickListener {
            mCheckUpdateModel.checkUpdate(mActivity)
        }
        if (SingletonUtils.get().isNeedUpdateApp){
            newVersionDot.visibility = View.VISIBLE
        }else{
            newVersionDot.visibility = View.GONE
        }



    }

    override fun onResume() {
        super.onResume()
        val userInfo = ProjectSpUtils.getUserInfo()
        userInfo?.run {
            if (this.isVisitor) {
                userHeadIv.setImageResource(R.drawable.user_img_empty_head)
                userNameTv.text = getString(R.string.place_login)

                loginOutBt.visibility = View.GONE

            } else {
                mViewModel.userInfo("${userInfo.user.id}")
            }
        }

    }


    override fun providerVMClass(): Class<UserInfoViewModel>? {
        return UserInfoViewModel::class.java
    }

    override fun observeVM() {
        mViewModel.mUpdateResponseBean.observe(this, Observer {

        })
        mViewModel.mUserInfosBean.observe(this, Observer {
            if (!isEmptyStr(it.avatar)) {
                ImageLoader.getInstance().loadImage(
                    mActivity, ImageConfigImpl.builder()
                        .isCircle(true)
                        .url(it.avatar)
                        .placeholder(R.drawable.user_img_empty_head)
                        .imageView(userHeadIv)
                        .build()
                )
            }
            if (!isEmptyStr(it.nickName)) {
                userNameTv.text = printNoNull(it.nickName)
            } else {
                userNameTv.text = getString(R.string.user_edit_info)
            }

        })
    }
}